#include"ChatServer.h"



User::User():allfriends{"root","zhong"}{}


void User::clean(string myfriend)
{
mymessage.erase(myfriend);
}
bool User::isfriend(string myfriend)
{
for(auto s:allfriends)
if(s==myfriend)
return 1;
return 0;

}
void User::addfriend(string myfriend)
{
allfriends.insert(myfriend);

}
string User::getfriend()
{
string str;
for(auto s:allfriends)
{
str+=" "+s;
}
return str;

}
void User::addmessage(string myfriend,pair<string,string> amsg)
{
mymessage[myfriend].insert(amsg);
}

void User::sent(int connfd,string myfriend)
{
for(auto s:mymessage[myfriend])
{
if(dealdata(connfd)=="ok")
write(connfd,s.second.c_str(),s.second.size());
if(dealdata(connfd)=="ok")
write(connfd,s.first.c_str(),s.first.size());
}
cout<<"test"<<endl;
string sbuff="end";
if(dealdata(connfd)=="ok")
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str()));
}
void User::getmsg(int connfd)
{
string sbuff;
for(auto s:mymessage)
{
if(dealdata(connfd)=="ok")
write(connfd,s.first.c_str(),s.first.size());
}
sbuff="end";
if(dealdata(connfd)=="ok")
write(connfd,sbuff.c_str(),sbuff.size());
}
/*
int User getmsgnum()
{
int i=0;
for(auto c:mymessage)
i++;
return i;

}*/
//class Post
Post::Post(){puser="0";ptime="0",ptitle="0",pdata="0";}
Post::Post(const string& user,const string& title,const string& time,const string& data):puser(user),ptime(time),ptitle(title),pdata(data){}
void Post::addpost(string user,string time,string title,string data)
{
puser=user;
ptime=time;
ptitle=title;
pdata=data;
}
string Post::getpuser()
{
return puser;
}
string Post::getptime()
{
return ptime;
}
string Post::getpdata()
{
return pdata;
}
string Post::getptitle()
{
return ptitle;
}
